package com.carkeeper.user.service.impl;


import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.user.dao.DepartmentMapper;
import com.carkeeper.user.dao.EmployeeMapper;
import com.carkeeper.user.pojo.Department;
import com.carkeeper.user.pojo.Employee;
import com.carkeeper.user.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;
    //查询全部
    @Override
    public List<Employee> findAll() {
        //先查询数据库表里本来有的字段
        List<Employee> allByEmployee = employeeMapper.findAllByEmployee();
        //遍历员工集合拿出每个员工对象
        for (Employee employee : allByEmployee) {
            //根据id查询部门表中对应的部门对象
            Department department = departmentMapper.selectByPrimaryKey(employee.getDepartmentId());
            //创建部门对象封装部门名称
            Department departments = new Department();
            //获取根据id查出来的部门名称
            departments.setName(department.getName());
            //封装值进employee
            employee.setDepartments(departments);
        }


        return allByEmployee;
    }

    /**
     * 增加员工
     * @param employee
     *
     */
    @Override
    public void add(Employee employee) {
        //新增创建时间
        employee.setCreateTime(new Date());
       employeeMapper.insert(employee);
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @Override
    public Employee findById(Integer id) {

        return employeeMapper.findById(id);
    }

    //更新员工状态
    @Override
    public void update(Employee employee) {
        employee.setUpdateTime(new Date());
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    //根据条件搜索
    @Override
    public List<Employee> search(int pageSize, int currentPage, Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return employeeMapper.selectByExample(example);

    }

    //锁定账号状态
    @Override
    public Result lock(Integer id) {

        //先根据id查询用户状态
         Employee employee = new Employee();
         Integer  accountStatusById=employee.getAccountStatus();
         accountStatusById = employeeMapper.findAccountStatusById(id);
        if (1==accountStatusById){
               //当前状态为1
               employee.setAccountStatus(2);
               employee.setUpdateTime(new Date());
               //修改数据库中信息
               employeeMapper.updateByPrimaryKeySelective(employee);
               return new Result(true,StatusCode.OK,"成功锁定用户");
        }else {
            return new Result(false,StatusCode.ERROR,"该用户已被锁定");
        }

    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String,Object> searchMap){
        Example example = new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            //构建姓名模糊查询条件
            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            //构建手机号查询条件
            if (searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))){
                criteria.andEqualTo("phone",searchMap.get("phone"));
            }
            //构建电子邮件查询条件
            if (searchMap.get("email")!=null && !"".equals(searchMap.get("email"))){
                criteria.andEqualTo("email",searchMap.get("email"));
            }
            //构建所属部门查询条件
            if (searchMap.get("department_id")!=null && !"".equals(searchMap.get("department_id"))){
                criteria.andEqualTo("department_id",searchMap.get("department_id"));
            }
            //构建状态查询条件
            if (searchMap.get("account_status")!=null && !"".equals(searchMap.get("account_status"))){
                criteria.andEqualTo("account_status",searchMap.get("account_status"));
            }


           /* //构建手机号查询条件
            String phone = (String) searchMap.get("phone");
            if (StringUtils.isNotBlank(phone)) {
                criteria.andEqualTo("phone", phone);
            }
            //构建电子邮件查询条件
            String email = (String) searchMap.get("email");
            if (StringUtils.isNotBlank(email)) {
                criteria.andEqualTo("email", email);
            }
            //构建所属部门查询条件
            String department_id = (String) searchMap.get("department_id");
            if (StringUtils.isNotBlank(department_id)) {
                criteria.andEqualTo("department_id", department_id);
            }
            //构建状态查询条件
            String account_status = (String) searchMap.get("account_status");
            if (StringUtils.isNotBlank(account_status)) {
                criteria.andEqualTo("account_status", account_status);
            }*/

        }
        return example;
    }


}
